﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;

namespace Fdd.Aspen.Client.ViewModel.Infrastructure
{
    public class ViewModelLocator
    {
        public ViewModelLocator()
        {
            CompositionInitializer.SatisfyImports(this);
        }

        
        [ImportMany("ViewModel", AllowRecomposition = true)]
        public IEnumerable<ExportFactory<object, IViewModelMetadata>> ViewModelsFactories { get; set; }


        private object GetViewModel(string viewModel)
        {
            var context = ViewModelsFactories.Single(v => v.Metadata.Name.Equals(viewModel))
                                             .CreateExport();

            return context.Value;
        }


        public object this[string viewModel]
        {
            get { return GetViewModel(viewModel); }
        }


        public ViewModelLocator Find
        {
            get { return this; }
        }
    }
}